<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
  components: {
    SelectAddress: async () =>
      Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectAddress()),
    SelectDateCycleApplication: async () =>
      Vue.extend(
        await Vue.prototype.$vFN.Components.Base.SelectDateCycleApplication()
      ),
    SelectDataUsers: async () =>
      Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectDataUsers()),
  },
  data() {
    return {
      appWindow: window,
      form: {
        isOpen: 1, //状态 默认开启
        name: "", //自提点名称
        mobile: "", //电话
        memberIdLst: "", //管理员id数组
        detailAddress: "", //详细地址
        operateList:[],
      },
      operateList: [], // 营业时间数组

      // 省市区街
      addressData: {
        city: "",
        cityCode: "",
        province: "",
        provinceCode: "",
        region: "",
        regionCode: "",
        street: "",
        streetCode: "",
      },
      // 页面接口地址
      apiUrl: {
        create: "mall-site-portal/offline-address/create",
        update: "mall-site-portal/offline-address/update",
        detail: "mall-site-portal/offline-address/getItem",
      },

      usersIds: [], //会员id

      loading: false,
    };
  },
  watch: {
    usersIds(newArr, oldArr) {
      if (newArr.length > 0) {
        this.form.memberIdLst = newArr;
      } else {
        this.form.memberIdLst = "";
      }
    },
  },
  methods: {
    init() {
      this.mixinEdit_editVisible = true;
      if (this.type === "create") {
        this.loading = false;
        this.mixinEdit_editTitle = "添加自提点";
      } else {
        this.mixinEdit_editTitle = "修改自提点";
        this.getDetail();
      }
    },

    async getDetail() {
      this.loading = true;
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.detail,
          data: {
            id: this.id, //home页传入
          },
        },
        window
      );

      if (res._ok) {
        for (const key in this.form) {
          if (!this.$vFN.Utils.isEmpty(res.data[key])) {
            this.form[key] = res.data[key];
          }
        }

        for (const key in this.addressData) {
          if (!this.$vFN.Utils.isEmpty(res.data[key])) {
            this.addressData[key] = res.data[key];
          }
        }

        this.usersIds = res.data.memberIdLst;
        this.form.operateList = JSON.parse(res.data.operateList);
      }

      this.loading = false;
    },

    async submit() {
      if (!(await this.validate())) {
        return;
      }

      if (this.$vFN.Utils.isEmpty(this.form.operateList)) {
        this.$vFN.Tools.messageError("请选择营业时间");
        return;
      }

      if (this.addressData.province == "") {
        this.$vFN.Tools.messageError("请选择省市区街道");
        return;
      }

      let url_ = "";
      let dataObj_ = {
        ...this.form,
        ...this.addressData,
        operateList: JSON.stringify(this.form.operateList),
      };
      if (this.type == "create") {
        url_ = this.apiUrl.create;
      } else {
        url_ = this.apiUrl.update;
        dataObj_.id = this.id;
      }

      this.loading = true;
      let res = await this.$vFN.Http.post(
        {
          url: url_,
          data: dataObj_,
        },
        window
      );

      if (res._ok) {
        this.$vFN.Tools.messageSuccess(res.message);
        this.onSuccess();
        this.mixinEdit_editVisible = false;
      }
      this.loading = false;
    },

    validate() {
      let that = this;
      return new Promise((resolve, reject) => {
        that.$refs["editForm"].validate((valid) => {
          if (valid) {
            resolve(true);
          } else {
            resolve(false);
          }
        });
      });
    },

    // 成功回调
    onSuccess() {},
    // 取消回调
    onCancel() {},
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>